//
//  GroupsViewController.m
//  UADE-Mobile
//
//  Created by Marcos Damian Pianelli on 11/2/11.
//  Copyright (c) 2011 DG. All rights reserved.
//

#import "GroupsViewController.h"
#import "TTGroupsDataSource.h"

@implementation GroupsViewController
@synthesize aulaID, userInfo;

-(void)openDownloadSection{
    
    NSLog(@"%@",[[self.userInfo objectForKey:@"Archivos"] description]);
    
    [[TTNavigator navigator] openURLAction:
     [[[TTURLAction actionWithURLPath:@"tt://download"] applyQuery:[self.userInfo objectForKey:@"Archivos"]] applyAnimated:TRUE]];
    
}

-(id)initWithID:(NSString*)IDAula{
    
    
    if (self = [super init]) {
        
        self.aulaID = IDAula;
        
       /* self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(closeAction:)];
        */
        
        self.variableHeightRows = TRUE;
        self.title = NSLocalizedString(@"Group",@"");
        self.tableViewStyle = UITableViewStyleGrouped;

        TTURLMap* map = [TTNavigator navigator].URLMap;
        [map from:@"tt://downloadAction" toObject:self selector:@selector(openDownloadSection)];
        
    }
    
    return self;
    
    
    
    
}

-(void)didGetGroupInfo:(id)model withError:(NSError*)error{ 
    
    self.userInfo = model;
    
    
    if ( self.userInfo == nil ) {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.dataSource = [TTGroupsDataSource dataSourceWithArrays:nil];
        return;
    }    
    
    NSLog(@"%@",[[self.userInfo objectForKey:@"Archivos"] description]);
    
    NSDictionary * members = [self.userInfo objectForKey:@"Integrantes"];
    
    NSMutableArray * memberItems = [NSMutableArray array];
    
    for(NSDictionary * eachMember in members ){
        
        [memberItems addObject:[TTTableTextItem itemWithText:[eachMember objectForKey:@"Nombre"]]];
         
    }
    self.title = [self.userInfo objectForKey:@"Nombre"];

    
    NSString * messagesURL =  [NSString stringWithFormat:@"tt://messages/%@",self.aulaID];

    
    NSArray * optionTableItems = [NSArray arrayWithObjects:
                                   
                                   [TTTableImageItem itemWithText:NSLocalizedString(@"Messages", @"") imageURL:@"bundle://btn_foro.jpg"
                                                              URL:messagesURL],
                                   
                                   [TTTableImageItem itemWithText:NSLocalizedString(@"Download", @"") imageURL:@"bundle://btn_descargar.jpg" 
                                                              URL:@"tt://downloadAction"],  nil];
    

    
    self.dataSource = [TTGroupsDataSource dataSourceWithArrays:
                       NSLocalizedString(@"Members", @""),
                       memberItems,
                       NSLocalizedString(@"Options", @""),
                       optionTableItems,
                       nil];

    
    
}




- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[StudentModel SharedInstance] fetchGroupWithDelegate:self aulaID:self.aulaID];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    TTURLMap* map = [TTNavigator navigator].URLMap;
    [map removeURL:@"tt://downloadAction"];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
